package tool;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Properties;
import java.util.UUID;

import javax.imageio.ImageIO;

import com.mengtian_tech.smart_lab.util.ImageUtils;

/**
 * 操作图片的工具类
 *
 */
public class ImageFileTools {
	public static final String IMG_BASE_DIR = "image_file_base_dir";
	private static String baseDir = null; 
	 
	/**
	 * 创建一个文件，文件路径基于配置
	 * @param parentDirName   父文件夹名称
	 * @param fileName
	 * @return
	 */
	public static  File createImgFile(String parentDirName)throws IOException{
		String dir = getBaseDir();
		File baseDirFile = new File(dir,parentDirName);
		if(!baseDirFile.exists()) baseDirFile.mkdirs();
		
		File file = new File(baseDirFile,UUID.randomUUID().toString());
		file.createNewFile();
		return file;
	}
	
	private static String getBaseDir() throws IOException {
		if(baseDir == null){
			Properties props = new Properties();
			props.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("config.properties"));
			baseDir = props.getProperty(IMG_BASE_DIR);
		}
		return baseDir;
	}

	public static File findImgFile(String fileName)throws IOException{
		File file = null;
		if(fileName == null){
			return null;
		}else{
			file = new File(getBaseDir(),fileName);
		} 
		
		if(file.exists()){
			return file;
		}else{
			return null;
		}  
	} 
	
	public static File saveFileFromInputStream(String parentDirName,InputStream is) throws IOException{
		File outFile = createImgFile(parentDirName);
		FileOutputStream fos = new FileOutputStream(outFile);
		try{
		byte[] buffer = new byte[1024];
		int count = is.read(buffer);
		while (count > 0){
			fos.write(buffer,0,count);
			count = is.read(buffer);
		} 
		
		}finally{
			fos.close();
		}
		
		return outFile;
	}
	
	public static File saveFileFromByteArray(String parentDirName,byte[] data) throws IOException{
		File outFile = createImgFile(parentDirName);
		FileOutputStream fos = new FileOutputStream(outFile);
		try{
			fos.write(data,0,data.length); 
		}finally{
			fos.close();
		}
		
		return outFile;
	}
	
	public static File saveSignFileFromInputStream(String parentDirName,InputStream is) throws Exception{
		File outFile = createImgFile(parentDirName);
		FileOutputStream fos = new FileOutputStream(outFile);
		try{
		byte[] buffer = new byte[1024];
		int count = is.read(buffer);
		while (count > 0){
			fos.write(buffer,0,count);
			count = is.read(buffer);
		} 
		
		}finally{
			fos.close();
		}
		
		
		FileInputStream in = new FileInputStream(outFile);
		byte[] data = ImageUtils.transferAlpha(ImageIO.read(outFile), in, 200);		
		in.close();		
		
		
		//处理后的图片保存
		FileOutputStream newOutFile = new FileOutputStream(outFile.getAbsolutePath());
		newOutFile.write(data);
		newOutFile.close();
		
		return outFile;
	}
	
	public static void writeFileToOutpustStream(File file,OutputStream os)throws IOException{
		 
		FileInputStream fis = null;
		try {
			if (file != null) { 
				byte[] buffer = new byte[1024];
				fis = new FileInputStream(file); 
				int count = fis.read(buffer, 0, buffer.length);
				while (count > 0) {
					os.write(buffer, 0, count); 
					count = fis.read(buffer, 0, buffer.length);
				} 
			}

			os.flush();

		}  finally {
			if (fis != null){
				try {
					fis.close();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
	}
}
